//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdmLinearOpComposite.h"
using namespace Cephei::QL::Methods::Finitedifferences::Operators;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Methods/Finitedifferences/Operators/FdmLinearOp.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpComposite::CFdmLinearOpComposite (boost::shared_ptr<QuantLib::FdmLinearOpComposite>& childNative, Object^ owner) : CFdmLinearOp(CFdmLinearOpComposite::typeid)
{
#ifdef HANDLE
	_phFdmLinearOpComposite = NULL;
#endif
	_ppFdmLinearOpComposite = &childNative;
    _ppFdmLinearOp = new boost::shared_ptr<QuantLib::FdmLinearOp> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppFdmLinearOpComposite));
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpComposite::CFdmLinearOpComposite (QuantLib::FdmLinearOpComposite& childNative, Object^ owner) : CFdmLinearOp(CFdmLinearOpComposite::typeid)
{
#ifdef HANDLE
	_phFdmLinearOpComposite = NULL;
#endif
	_ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (&childNative);
    _ppFdmLinearOp = new boost::shared_ptr<QuantLib::FdmLinearOp> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppFdmLinearOpComposite));
    _FdmLinearOpCompositeOwner = owner;
    _FdmLinearOpOwner = owner;
}

Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpComposite::CFdmLinearOpComposite (CFdmLinearOpComposite^ copy) : CFdmLinearOp(CFdmLinearOpComposite::typeid)
{
#ifdef HANDLE
	_phFdmLinearOpComposite = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (copy->GetShared());
        _ppFdmLinearOp = new boost::shared_ptr<QuantLib::FdmLinearOp> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppFdmLinearOpComposite));
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpComposite::CFdmLinearOpComposite (PLATFORM::Type^ t) : CFdmLinearOp(CFdmLinearOpComposite::typeid)
{
#ifdef HANDLE
	_phFdmLinearOpComposite = NULL;
#endif
	if (!t->IsSubclassOf(CFdmLinearOpComposite::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpComposite::CFdmLinearOpComposite (QuantLib::Handle<QuantLib::FdmLinearOpComposite>& childNative, Object^ owner)  : CFdmLinearOp(CFdmLinearOpComposite::typeid)
{
	_phFdmLinearOpComposite = &childNative;
	_ppFdmLinearOpComposite = &static_cast<boost::shared_ptr<QuantLib::FdmLinearOpComposite>>(childNative.currentLink());
    _ppFdmLinearOp = new boost::shared_ptr<QuantLib::FdmLinearOp> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppFdmLinearOpComposite));
    _FdmLinearOpCompositeOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpComposite::CFdmLinearOpComposite (QuantLib::Handle<QuantLib::FdmLinearOpComposite> childNative)  : CFdmLinearOp(CFdmLinearOpComposite::typeid)
{
	_phFdmLinearOpComposite = &childNative;
	_ppFdmLinearOpComposite = &static_cast<boost::shared_ptr<QuantLib::FdmLinearOpComposite>>(childNative.currentLink());
    _ppFdmLinearOp = new boost::shared_ptr<QuantLib::FdmLinearOp> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppFdmLinearOpComposite));
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpComposite::CFdmLinearOpComposite (QuantLib::FdmLinearOpComposite childNative)  : CFdmLinearOp(CFdmLinearOpComposite::typeid)
{
#ifdef HANDLE
	_phFdmLinearOpComposite = NULL;
#endif
	_ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (new QuantLib::FdmLinearOpComposite (childNative));
    _ppFdmLinearOp = new boost::shared_ptr<QuantLib::FdmLinearOp> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppFdmLinearOpComposite));
}
#endif

Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpComposite::~CFdmLinearOpComposite ()
{
    if (_ppFdmLinearOpComposite != NULL)
    {
	    delete _ppFdmLinearOpComposite;
        _ppFdmLinearOpComposite = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpComposite::!CFdmLinearOpComposite ()
{
    if (_ppFdmLinearOpComposite != NULL)
    {
	    delete _ppFdmLinearOpComposite;
    }
}
QuantLib::FdmLinearOpComposite& Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpComposite::GetReference ()
{
    if (_ppFdmLinearOpComposite == NULL) throw REFNEW NativeNullException ();
	return **_ppFdmLinearOpComposite;
}
boost::shared_ptr<QuantLib::FdmLinearOpComposite>& Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpComposite::GetShared ()
{
    if (_ppFdmLinearOpComposite == NULL) throw REFNEW NativeNullException ();
	return *_ppFdmLinearOpComposite;
}
QuantLib::FdmLinearOpComposite* Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpComposite::GetPointer ()
{
    if (_ppFdmLinearOpComposite == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdmLinearOpComposite;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmLinearOpComposite>& Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpComposite::GetHandle ()
{
	if (_phFdmLinearOpComposite == NULL)
	{
		_phFdmLinearOpComposite = new Handle<QuantLib::FdmLinearOpComposite> (*_ppFdmLinearOpComposite);
	}
	return *_phFdmLinearOpComposite;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpComposite::HasNative () 
{
	return (_ppFdmLinearOpComposite != NULL);
}

Cephei::QL::Methods::Finitedifferences::Operators::IFdmLinearOpComposite^ Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpComposite::SetTime (Double t1, Double t2)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Time _t1 = (QuantLib::Time)ValueHelper::Convert (t1); //a
        QuantLib::Time _t2 = (QuantLib::Time)ValueHelper::Convert (t2); //a
    	(*_ppFdmLinearOpComposite)->setTime ( _t1,  _t2 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpComposite::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppFdmLinearOpComposite)->size ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

